import sys
from PyQt5.QtWidgets import (
    QApplication,
    QWidget,
    QLabel,
    QLineEdit,
    QPushButton,
    QVBoxLayout,
    QHBoxLayout,
    QGridLayout,
    QSpacerItem,
    QSizePolicy,
)
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer, QDateTime, Qt
from util.config import Config
from PyQt5.QtGui import QPixmap, QPalette, QBrush, QFont
import os 


class LoginScreen(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle("登录界面")
        self.setGeometry(0, 0, 680, 400)
        #self.setFixedSize(680, 500)  # 设置窗口大小为400x300
        
        #self.setWindowFlag(Qt.FramelessWindowHint)  # 无边框
        #self.setMouseTracking(True)  # 启用鼠标跟踪
        #self.setAttribute(Qt.WA_TranslucentBackground)  # 设置背景透明
        #self.setStyleSheet("background: transparent;")  # 设置样式表背景
        #self.setStyleSheet("#MainWindow{background-color:#00B7FD}")
        # 去掉最大变化、最小化及关闭按钮
        #self.setWindowFlags(Qt.WindowCloseButtonHint|Qt.WindowStaysOnTopHint|Qt.WindowCloseButtonHint)
        # 设置窗口标志，去掉标题栏
        #self.setWindowFlags(Qt.FramelessWindowHint)

        # 初始化配置
        self.config = Config()

        # 设置背景图片
        self.setAutoFillBackground(True)
        palette = self.palette()
        background = QPixmap(self.config.LOGIN_BG)
        palette.setBrush(QPalette.Background, QBrush(background.scaled(self.size(), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)))
        
        self.setPalette(palette)

        # 创建顶部区域topview
        topview = QWidget(self)
        #topview.setFixedHeight(50)  # 设置顶部区域的固定高度

        # 设置顶部区域的布局
        top_layout = QVBoxLayout(topview)
        top_layout.setGeometry(QtCore.QRect(0, 0, 150, 30))
        
        # time_label：显示当前时间，并且距离左边20px
        self.time_label = QLabel(topview)
        #self.time_label.setGeometry(QtCore.QRect(0, 0, 80, 30))
        self.time_label.setFont(QFont("Arial", 10))
        #self.time_label.setStyleSheet("color: white;")
        self.time_label.setAlignment(Qt.AlignLeft)
        self.time_label.setStyleSheet("""
        QLabel {
            background: transparent;   /*背景透明 */
            color: white;  /* 文本颜色 */
            margin-top:-5px;
            margin-left:60px;
        }
    """)
        self.time_label.setText('当前时间')
        self.update_time()  # 初始化时间显示
        timer = QTimer(self)
        timer.timeout.connect(lambda: self.update_time())  # 定时更新时间
        timer.start(1000)  # 每秒更新时间
        
        # 设置布局
        top_layout.addWidget(self.time_label)
        top_layout.setSpacing(10)  # 设置组件之间的间距
        
        # 表单容器
        self.form_container = QWidget(self)
        #self.form_container.setFixedSize(220, 140)  # 固定大小为 300*400
        
        #self.form_container.setStyleSheet("background: rgba(255, 255, 255, 0.8); border-radius: 10px;")
        self.form_container.setMaximumWidth(400)
        
        self.form_container.setStyleSheet("""
            QWidget {
                border-radius: 10px;
                background-image: url('views/assets/login_bg2.png'); /* 替换为你的图片路径 */
                background-repeat: no-repeat;          /* 不重复 */
                background-position: center;          /* 居中显示 */
               
                
                /*image: url(views/assets/login_bg2.png);
                background-color:rgba(0,0,0,0);    */
            }
        """)


        # 表单布局
        self.form_layout = QGridLayout(self.form_container)
        # 用户名
        username_label = QLabel("用户名:")
        #username_label.setFont(QFont("Arial", 12))
        #检查是否有其他控件（如父控件）叠加样式或使用阴影效果。
        #使用 setAttribute(Qt.WA_TranslucentBackground, True) 设置控件属性，彻底去除背景：
        username_label.setAttribute(Qt.WA_TranslucentBackground, True)
        username_label.setStyleSheet("""
        QLabel {
            background: transparent;
            color: white; 
            font-size: 12px;
            margin-left:10px;
            margin-top:10px;
        }
    """)
        username_input = QLineEdit()
        username_input.setPlaceholderText("请输入用户名")
        username_input.setAttribute(Qt.WA_TranslucentBackground, True)
        username_input.setStyleSheet("""
            QLineEdit {
                background: white;
                font-size: 12px;
                margin-right:20px;
                margin-top:10px;
            }
        """)
        self.form_layout.addWidget(username_label, 0, 0)
        self.form_layout.addWidget(username_input, 0, 1)

        # 密码
        password_label = QLabel("密码:")
        password_label.setAttribute(Qt.WA_TranslucentBackground, True)
        password_label.setStyleSheet("""
            QLabel {
                background: white;
                color: white; 
                font-size: 12px;
                margin-left:10px;
                margin-top:10px;
            }
        """)
        #password_label.setFont(QFont("Arial", 10))
        password_input = QLineEdit()
        password_input.setPlaceholderText("请输入密码")
        password_input.setAttribute(Qt.WA_TranslucentBackground, True)
        password_input.setEchoMode(QLineEdit.Password)
        password_input.setStyleSheet("""
            QLineEdit {
                background: white; 
                font-size: 12px;
                margin-right:20px;
                margin-top:10px;
            }
        """)
        self.form_layout.addWidget(password_label, 1, 0)
        self.form_layout.addWidget(password_input, 1, 1)

        # 登录按钮
        login_button = QPushButton("登录")
        login_button.setFont(QFont("Arial", 10))
        login_button.setAttribute(Qt.WA_TranslucentBackground, True)
        login_button.clicked.connect(self.login_event)
        #login_button.setFixedSize(100, 30)  # 设置按钮大小
       
        # 设置按钮样式：去掉边框，设置背景图片，并自适应按钮大小
        '''
        login_button.setStyleSheet("""
            QPushButton {
                background-color: #4CAF50; color: white;
                font-size: 12px;margin-left:20px;margin-right:50px;margin-top:10px;
                
                /*background: transparent;   去掉上边框 */
                /*image: url(views/assets/login_btn.png);   左边框 */
                
            }
        """)'''
        
        login_button.setStyleSheet("""
            QPushButton {
                background-color: #516772;  /* 普通背景色 */
                color: white;              /* 普通文字颜色 */
                font-size: 12px;           /* 字体大小 */
                border-radius: 10px;       /* 圆角 */
                padding: 10px 20px;        /* 内边距 */
                margin-left:20px;
                margin-right:50px;
                margin-top:10px;
            }
            QPushButton:hover {
                background-color: #747d8e; /* 悬停时背景色 */
            }
            QPushButton:pressed {
                background-color: #707a87; /* 点击时背景色 */
                padding-left: 15px;        /* 按下时的微调效果 */
                padding-top: 12px;
            }
        """)
        
        self.form_layout.addWidget(login_button, 2, 1,1,1)  # 按钮占两列
        
        # 主布局
        main_layout = QVBoxLayout(self)
        main_layout.setContentsMargins(0, 0, 0, 0)  # 去掉主布局的边距
        main_layout.setSpacing(0)  # 去掉组件之间的间隙
        main_layout.addWidget(topview)  # 添加顶部区域
        main_layout.addStretch()  # 占位符，保持时间标签在顶部

        # 水平布局使表单居中
        center_layout = QHBoxLayout()
        center_layout.addStretch()  # 左侧占位符
        center_layout.addWidget(self.form_container)  # 添加表单
        center_layout.addStretch()  # 右侧占位符

        main_layout.addLayout(center_layout)
        main_layout.addStretch()  # 占位符，保持表单居中

        self.setLayout(main_layout)
        

    '''  
    #解决设置无边框不能拖动
    def mousePressEvent(self, event):
        # 记录鼠标按下时的位置
        self.offset = event.pos()
 
    def mouseMoveEvent(self, event):
        # 移动窗口位置
        x = event.globalX()
        y = event.globalY()
        x_w = self.offset.x()
        y_w = self.offset.y()
        self.move(x - x_w, y - y_w)
        '''  
        
    def update_time(self):
        """更新时间"""
        current_time = QDateTime.currentDateTime()
        time_str = current_time.toString("yyyy-MM-dd HH:mm:ss")
        self.time_label.setText(time_str)
        
    def update_time(self):
        """更新顶部时间显示"""
        current_time = QDateTime.currentDateTime()
        time_str = current_time.toString("yyyy-MM-dd HH:mm:ss")
        self.time_label.setText(time_str)

    def resizeEvent(self, event):
        """调整背景图片大小"""
        palette = self.palette()
        background = QPixmap(self.config.LOGIN_BG)
        palette.setBrush(QPalette.Background, QBrush(background.scaled(self.size(), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)))
        self.setPalette(palette)
        
        super().resizeEvent(event)
        
    def login_event(self):
        print('login_event')

'''
if __name__ == "__main__":
    app = QApplication(sys.argv)
    login_screen = LoginScreen()
    login_screen.show()
    sys.exit(app.exec_())
'''